﻿package com.firewolf.spring.transaction.annotation;

import java.util.Arrays;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AnnotationMainDriver {
	
	private ApplicationContext ctx = null;
	
	private IBookShopDao bookDao = null;
	
	@Autowired
	private BookShopService service = null;
	
	{
		ctx = new ClassPathXmlApplicationContext("annotation.xml");
		bookDao = ctx.getBean(BookDaoImpl.class);
		service = ctx.getBean(BookShopService.class);
	}
	
	@Test
	public void testFindPriceById() throws Exception {
		System.out.println(bookDao.findBookPriceById("1001"));
	}
	
	@Test
	public void testUpdateBookStock() throws Exception{
		bookDao.updateBookStock("1001");
	}
	
	@Test
	public void testUpdateUserAccount() throws Exception{
		int price = bookDao.findBookPriceById("1001");
		bookDao.updateUserAccount("liuxing", price);
	}
	
	/**
	 * 测试事务
	 * @throws Exception
	 */
	@Test
	public void testBuyBook() throws Exception{
		service.buyBook("1001", "liuxing");
	}
	
	@Test
	public void testBatchBuyBook() throws Exception{
		BatchBookShopService batchService = (BatchBookShopService) ctx.getBean("batchService");
		batchService.bacthBuyBooks("liuxing", Arrays.asList("1001","1002"));
	}
}
